Vue.component("health_analysis", {
	props: ["parent_data"],
	template: '#health_analysis',
	data: function() {
		return {
			questions: [],
			url: {
				getPageCurrencyRates: '/api/CurrencyRate/GetPageCurrencyRates', //获取货币种类
				getAllTDCategory: '/api/TreasureDiagnosisCategory/GetAllTDCategory', //获取所有资产类型
				saveTDCategoryValues:'/api/TreasureDiagnosisCategory/SaveTDCategoryValues', //保存健康分析问题
				getSpecialAnswers:'/api/UserQuestion/GetSpecialAnswers', //获取问题答案
				getProductPrices:'/api/Product/GetProductPrices',//获取产品价格
				genOrder:"/api/Order/GenOrder",//获取流水号
			},
			self_question_step:0, //题目编号
			answer_items: [],
			currencyRates: [], //货币种类
			my_questions:[],//问题
			isFirst:true, //是否是第一道题
			isLast:false, //是否最后一道题
			before_answers:[], //之前做题答案
		}
	},
	computed: {

	},
	mounted: function() {
		var that = this;
		//获取货币种类
		that.getPageCurrencyRates()
		console.log(that.parent_data.CustomerID)
	},
	methods: {
		getPageCurrencyRates: function() {
			var that = this
			var data = { PageIndex: 1, PageSize: 10000 }
			globalData.ajaxPost(that.url.getPageCurrencyRates, data, function(res) {
				if(res.StatusCode == 200) {
					console.log(res)
					if(res.data) {
						that.currencyRates = res.data
						console.log(that.currencyRates)
						//获取之前的答题答案
						that.getSpecialAnswers()
						
					}
				}
			})
		},
		getAllTDCategory: function() {
			var that = this
			var data = { "CategoryType": "" }
			globalData.ajaxPost(that.url.getAllTDCategory, data, function(res) {
				if(res.StatusCode == 200) {
					console.log(res)
					if(res.data) {
						that.my_questions = that.reorganization(res.data)
						console.log(that.my_questions)
						setTimeout(function(){		
							console.log(edit_password_valid_json)
							$('.form_health_analysis').bootstrapValidator(edit_password_valid_json);		
						},1000)	
					}
				}
			})
		},
		getSpecialAnswers:function(){
			var that = this;
			var data = {CustomerID:that.parent_data.CustomerID,
						ModelID:'2'}
			globalData.ajaxPost(that.url.getSpecialAnswers, data, function(res) {
				if(res.StatusCode == 200) {
					console.log(res)
					if(res.data) {
						$.each(res.data,function(index,item){
							if(item.length>0){
								that.before_answers = item
							}							
						})
						that.getAllTDCategory()
					}
				}
			})
		},
		reorganization: function(data) {
			var that = this
			if(!data) {
				return {};
			}
			if(that.before_answers.length>0){
				//将之前答案赋值
				$.each(data,function(index,item){
					$.each(that.before_answers, function(i,val) {
						if(val.QuestionID == item.ID){
							item.AnswerValue1 = val.AnswerValue
							item.CurrencyID  = val.CurrencyID
							return;
						}
					});
				})
				console.log(data)
			}
			
			//三层结构			
			var out = [{
				CategoryTypeName: data[0].CategoryTypeName,
				CategoryType: data[0].CategoryType,
				types: [{
						"ParentID": data[0].ParentID,
						"ParentAssetCategory": data[0].ParentAssetCategory,
						data: [{
							ID: data[0].ID,
							AssetCategory: data[0].AssetCategory,
//							CurrencyID: (that.before_answers.length>0 ? item.CurrencyID : that.currencyRates[0].ID),
							CurrencyID: (that.before_answers.length>0 ? data[0].CurrencyID : "a9513a8f-db8d-47a9-95d5-a283651866e0"),//人民币					
							AnswerValue1: data[0].AnswerValue1
						}]
					}
				]
			}]
			
			$(data).each(function(index, item) {
				if(index > 0) {
					$(out).each(function(n, value) {
						if(item.CategoryType == value.CategoryType) {
							$(value.types).each(function(i, item_m) {
								if(item_m.ParentID == item.ParentID) {
									item_m.data.push({
										ID: item.ID,
										AssetCategory: item.AssetCategory,
										CurrencyID: (that.before_answers.length>0 ? item.CurrencyID : "a9513a8f-db8d-47a9-95d5-a283651866e0"),
										AnswerValue1: item.AnswerValue1
									})
									return false;
								} else {
									if(i == value.types.length - 1) {
										value.types.push({
											ParentID: item.ParentID,
											ParentAssetCategory: item.ParentAssetCategory,
											data: [{
												ID: item.ID,
												AssetCategory: item.AssetCategory,
												//判断是否之前答过题
												CurrencyID: (that.before_answers.length>0 ? item.CurrencyID : "a9513a8f-db8d-47a9-95d5-a283651866e0"),
												AnswerValue1: item.AnswerValue1
											}]
										})
									}
								}
							})
						} else {
							if(n == out.length - 1) {
								out.push({
									CategoryTypeName: item.CategoryTypeName,
									CategoryType: item.CategoryType,
									types: [{
											ParentID: item.ParentID,
											ParentAssetCategory: item.ParentAssetCategory,
											data: [{
												ID: item.ID,
												AssetCategory: item.AssetCategory,
												CurrencyID: (that.before_answers.length>0 ? item.CurrencyID : "a9513a8f-db8d-47a9-95d5-a283651866e0"),
												AnswerValue1: item.AnswerValue1
											}]
										}
									]
								})
							}
						}
					})
				}
				var name = '_'+item.ID.replace(/-/g,'_')
				edit_password_valid_json.fields[name]= {
					validators: {
						notEmpty: {
							message: '数值不能为空'
						}
					}
				}
			})
			return out;
		},
		go_next_step:function(index){
			var that = this
			$('.form_health_analysis').bootstrapValidator('validate');
			var b = $('.form_health_analysis').data("bootstrapValidator").isValid();
			if(!b) {
				return;
			}
			//判断是否是现有财富健康分析		
				globalData.utils.scrool_top() //滚动到顶部
				that.isFirst = false;
				if(that.self_question_step == that.my_questions.length-2){
					that.isLast = true
				}
				if(that.self_question_step == that.my_questions.length-1){
					//判断是否签约
					if(globalData.userInfo.IsVip==0){
						$("#alert").modal()
						$('#alert p').text('尊敬的用户，您目前的身份为【注册用户】,尚无权限观看本项目的信息，请在个人中心中点击"签约用户"并上传证件照，通过审核后，您将成为【签约用户】且享有观看项目信息权限')
						return;
					}else if(globalData.userInfo.IsVip==3){
						$("#alert").modal()
						$("#alert p").text('您的签约资料已提交，正在审核中，请耐心等待')
						return
					}else if(globalData.userInfo.IsVip==2){
						$("#alert").modal()
						$("#alert p").text('您的签约资料审核未通过，请到个人中心重新提交签约资料！')
						return
					}
					
					
					console.log(that.my_questions)
					that.answer_items = []
					$.each(that.my_questions, function(index,item) {
						$.each(item.types, function(i,item_m) {
							$.each(item_m.data,function(n,item_mm){
								var obj = {
							        UserID: '',
							        TDAID: item_mm.ID,
							        CurrencyID: item_mm.CurrencyID,
							        CustomerID: that.parent_data.CustomerID,
							        AnswerValue1:item_mm.AnswerValue1
								}
								that.answer_items.push(obj)
							})
						});
					});
					console.log(that.answer_items)
					//上传题目
					var data = {
							Device:3,
							Answers:that.answer_items
						}
					globalData.ajaxPost(that.url.saveTDCategoryValues, data, function(res) {
						console.log(res)						
						if(res.suc) {						
							//发送专属问题数据，跳到分析页
							that.parent_data.evaluation_result = res.data
							globalData.ajaxPost(that.url.getProductPrices,{'ModelID':2},function(res) {
								if(res.suc) {
									console.log(res.data)									
									globalData.setStorage('product_money',res.data[0].Price)
									$("#pay_detail").modal()
									var tips = '查阅该健康分析需要消费'+res.data[0].Price+'金币'
									$("#pay_detail p").text(tips)
									
								}
							})
						}
					})
					return;
				}
				this.self_question_step = index+1
		},
		pay_money: function() {
			var that = this;
			var data = {
				'CargoName': '财富健康支付',
				'CargoNo': '',
				'CargoCount': '1',
				'Money': globalData.getStorage('product_money'),
				'Remark': '查阅健康支付||'+that.parent_data.CustomerID,
				'OrderType': '4'
			}
			globalData.ajaxPost("/api/Order/GenOrder", data, function(res) {
				if(res.suc) {
					that.parent_data.question_step = 3
					console.log()
				} else {
					$("#alert").modal()
					$("#alert p").text(res.msg)
				}		
			})		
		},
		go_pre_step:function(index){			
			if(index>0){
				this.self_question_step = index-1
				if(this.self_question_step==0){
					this.isFirst  = true;
				}
			}
			
		}
	},
	filters: {
		change_name:function(val){
			return ("_"+val.replace(/-/g,'_'));
		},
		reg_space:function(val){
			return (val+1)
		}
	}
})



//创建表单验证对象
var edit_password_valid_json = {
	message: 'This value is not valid',
	feedbackIcons: {
		valid: 'glyphicon glyphicon-ok',
		invalid: 'glyphicon glyphicon-remove',
		validating: 'glyphicon glyphicon-refresh'
	},
	fields: {
		oldPwd: {
			validators: {
				notEmpty: {
					message: '旧密码不能为空'
				}
			}
		}
	}
}